package com.face.controller;

import com.face.common.Page;
import com.face.common.Result;
import com.face.common.ResultCode;
import com.face.entity.Remark;
import com.face.exception.ArgumentException;
import com.face.exception.DatabaseException;
import com.face.exception.ServiceException;
import com.face.service.RemarkService;
import com.face.service.UserService;
import com.face.wrapper.RemarkWrapper;
import com.face.wrapper.UserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/remark")
public class RemarkController {
	
	@Autowired
    private RemarkService remarkService;
	@Autowired
	private UserService userService;
	
	@PostMapping("/add")
    public Result createRemark(@RequestBody Remark remark) {
//		System.out.println("用户id"+remark.getUserId());
		try{
			if(remark==null||remark.getDetail().equals("")){
				return Result.failure(ResultCode.BAD_PARAMETER);
			}
			remarkService.saveRemark(remark);
			String userId = remark.getUserId();
			UserWrapper user = userService.findByUserId(userId);
			Integer visitCount = user.getVisitCount();
			if (visitCount == null) {
				visitCount = 0;
			}
			user.setVisitCount(visitCount + 1);
			userService.updateUser(user);
		}catch(DatabaseException e){
			return Result.failure(ResultCode.CREATE_FAIL);
		}
		return Result.success(ResultCode.CREATED);
        
    }
	@GetMapping("/get/{userId}")
	public Result getRemarkByUserId(@RequestParam(value = "limit") Integer limit,@PathVariable String userId){
		UserWrapper wrapper = null;
		try{
			if(userId ==null){
				return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
			}
			if(StringUtils.isEmpty(userId)){
				return Result.failure(ResultCode.BAD_PARAMETER);
			}
			List<Remark> list =remarkService.getRemarksByUserId(userId,limit);
			if(list.size()==0){
				return Result.failure(ResultCode.NO_RECORDS);
			}
			wrapper= userService.getUserById(userId);
			
			wrapper.setRemarkList(list);

		}catch(ArgumentException  e){
			return Result.failure(ResultCode.BAD_PARAMETER);
		}catch(ServiceException e){
			return Result.failure(ResultCode.NO_USER_RECORD);
		}
		
		Result<UserWrapper> rs = new Result<UserWrapper>(ResultCode.QUERY_SUCCESS);
		rs.setData(wrapper);		
		return rs;
	}
	
	
	
	@GetMapping("/get/history/{userId}")
	public Result getRemarks(@RequestParam(value = "start") String start,@RequestParam(value = "end") String end,@PathVariable String userId){

		List<Remark> list=null;
		
		try{
			if(userId ==null){
				return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
			}
			if(StringUtils.isEmpty(userId)){
				return Result.failure(ResultCode.BAD_PARAMETER);
			}
			list =remarkService.getRemarks(userId,start,end);
			if(list.size()==0){
				return Result.failure(ResultCode.NO_RECORDS);
			}
			
		

		}catch(ArgumentException  e){
			return Result.failure(ResultCode.BAD_PARAMETER);
		}catch(ServiceException e){
			return Result.failure(ResultCode.NO_REMARK_RECORD);
		}
		Result<List> result =new Result<List>(ResultCode.QUERY_SUCCESS);
		result.setData(list);
		return result;
	}
	
	@GetMapping("/list")
	public Result list(String shopId,@RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit,
            @RequestParam(value = "start") String start,@RequestParam(value = "end") String end,
            String sort, String order){
		Page<RemarkWrapper> pageResult;
		try{
			if(shopId ==null){
				return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
			}
			if(StringUtils.isEmpty(shopId)){
				return Result.failure(ResultCode.BAD_PARAMETER);
			}
			pageResult =remarkService.list(shopId,page,limit,start,end,sort,order);
			if(pageResult.getTotal()==0){
				return Result.failure(ResultCode.NO_RECORDS);
			}

		}catch(ArgumentException  e){
			return Result.failure(ResultCode.BAD_PARAMETER);
		}catch(ServiceException e){
			return Result.failure(ResultCode.NO_USER_RECORD);
		}
		
		Result rs = new Result(ResultCode.QUERY_SUCCESS);
		rs.setData(pageResult);
		return rs;
	}
	@GetMapping("/count")
	public Result count(String shopId,String start,String end){
		if(shopId==null||shopId.isEmpty()){
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Integer count=remarkService.count(shopId,start,end);
		Result<Integer> result =new Result<Integer>(ResultCode.QUERY_SUCCESS);
		result.setData(count);
		return result;
		
	}

	@GetMapping("/newest")
	public Result record(String shopId, @RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "10")Integer limit,
						 @RequestParam(name = "start", required = false) String start,@RequestParam(name = "end", required = false) String end) {
		if(shopId==null||shopId.isEmpty()){
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Page<RemarkWrapper> result = remarkService.getNewestRemarkList(shopId, page, limit, start, end);
		return Result.success(result);
	}

	@GetMapping("/counts")
	public Result countList(String shopId, @RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "10") Integer limit,
							@RequestParam(name = "start", required = false) String start, @RequestParam(name = "end", required = false) String end) {
		if (shopId == null || shopId.isEmpty()) {
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Page<Map<String, Object>> result = remarkService.getRemarkCountList(shopId, page, limit, start, end);
		return Result.success(result);
	}

	@GetMapping("/list/{userId}")
	public Result listByUserId(@PathVariable("userId") String userId, @RequestParam("shopId") String shopId,
							   @RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "limit", defaultValue = "10") Integer limit,
							   @RequestParam(name = "start", required = false) String start, @RequestParam(name = "end", required = false) String end) {
		if(shopId==null||shopId.isEmpty()){
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Page<RemarkWrapper> result = remarkService.listAllByUserId(shopId, userId, page, limit, start, end);
		return Result.success(result);
	}

	/**
	 * 客户登记分析
	 * @param shopId
	 * @param start
	 * @param end
	 * @param type 1：按天 2：按月
	 * @return
	 */
	@GetMapping("/chart/{type}")
	public Result getChartData(@RequestParam("shopId") String shopId,@RequestParam(value = "start") String start,
							   @RequestParam(value = "end") String end, @PathVariable("type") Integer type) {
		if(shopId==null||shopId.isEmpty()){
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Map<String, Object> result = remarkService.getChartData(shopId, start, end, type);
		return Result.success(result);
	}
	/**
	 * 到访记录分析
	 * @param shopId
	 * @param start
	 * @param end
	 * @param type 1：按天 2：按月
	 * @return
	 */
	@GetMapping("/remark/{type}")
	public Result getRemarkCountData(@RequestParam("shopId") String shopId,@RequestParam(value = "start") String start,
							   @RequestParam(value = "end") String end, @PathVariable("type") Integer type) {
		if(shopId==null||shopId.isEmpty()){
			return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
		}
		Map<String, Object> result = remarkService.getRemarkCountData(shopId, start, end, type);
		return Result.success(result);
	}
}
